/**
	 * used for validating the email address
	 * @param str is inputEmail
	 * @returns boolean
	 */
function isEmail(str) {
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    return reg.test(str);
}
	
/**
         * function called when email(text_input) is changed
         */
function onchange_email() {
    var infor;
    if(isEmail(document.getElementById("email").value)){
        infor="";
    }else{
        infor="<b style=\"color:RED\">Email invalid</b>";
    }
    document.getElementById("infor_email").innerHTML=infor;
}
	
/**
         * function called when password(text_input) is changed
         */
function onchange_psw(){
    var infor;
    //alert(document.getElementById("password").value);
    if(document.getElementById("password").value==""){
        infor="<b style=\"color:RED\">Please input the password</b>";
    }else{
        infor="";
    }
    //alert(infor);
    document.getElementById("infor_psw").innerHTML=infor;
}

function validPswANDEmail(){
    var inforP;
    //alert(document.getElementById("password").value);
    if(document.getElementById("password").value==""){
        inforP="<b style=\"color:RED\">Please input the password</b>";
        return false;
    }else{
        inforP="";
        return true;
    }
    //alert(infor);
    document.getElementById("infor_psw").innerHTML=inforP;
    
    var inforE;
    if(isEmail(document.getElementById("email").value)){
        inforE="";
        return true;
    }else{
        inforE="<b style=\"color:RED\">Email invalid</b>";
        return false;
    }
    document.getElementById("infor_email").innerHTML=inforE;
}

function onclick_logon(){
    if(!validPswANDEmail()){
        return;
    }
    var email=document.getElementById("email").value;
    var password=document.getElementById("password").value;
    var params={
        "email":email,
        "password":password
    }  ;   //pack information into json
    
    //alert(params);
    
    $.ajax({
        url: 'LoginServlet',
        type: "POST",
        data: params,
        error: callbackF,
        success: callbackT
    });
}

/**
 *call back function used when server call back
 */
function callbackT(data){
    alert(data.isPass);
    if(data.isPass=="TRUE"){     
        document.getElementById("serverResponse").innerHTML="<b>Logon Successed<br/><b>Loading......</b>";
        self.location='communicationPlatForm/comIndex.jsp';
    }else{     
        document.getElementById("serverResponse").innerHTML="<b>Logon failed</b><br/><b>Please check email or password</b>";
    }
}

/**
 *call back function used when server DONOT call back
 */
function callbackF(data){
    alert("the server does not recall, request failed!");
    
}

//functions used to shut up the window
var i=10

function closewin(){
    self.opener=null;
    self.close();
}

function clock(){
    i=i-1
    document.getElementById("serverResponse").innerHTML="This window would be shut up in "+i+" seconds!";
    if(i>0)setTimeout("clock();",1000);
    else closewin();
}